package com.bestcem.xm.member.controller;

import cn.hutool.core.util.StrUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.member.controller.param.HttpCreateMemberParam;
import com.bestcem.xm.member.controller.vo.CreateMemberVO;
import com.bestcem.xm.member.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 开放平台
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/8/29
 */
@Slf4j
@RestController
@RequestMapping("/member/platform")
public class OpenPlatformController {

    @Resource
    private MemberService memberService;


    /**
     * 联系人创建
     *
     * @param param
     * @return
     */
    @PostMapping("/create")
    public XmResultJson createMember(@Valid @RequestBody HttpCreateMemberParam param,
                                     HttpServletRequest request) {
        // 获取组织id 和联系人id
        String orgId = SecurityContextHolder.getOrgId();
        String userId = SecurityContextHolder.getUserId();
        param.setOrgId(orgId);
        param.setUserId(userId);
        ServiceResult<String> result = memberService.createMember(param);
        if (result.isSuccess()) {
            CreateMemberVO vo = new CreateMemberVO();
            vo.setId(result.getData());
            return XmResultJson.success(vo);
        } else {
            if (StrUtil.isNotEmpty(result.getData())) {
                return XmResultJson.fail(Integer.parseInt(result.getErrorCode()), result.getMsg());
            }
            return XmResultJson.fail(Integer.parseInt(result.getErrorCode()), result.getMsg());
        }
    }
}
